<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_nilai extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'c.id_krs_detail';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		//$kd_kurikulum = isset($_POST['kd_kurikulum']) ? strval($_POST['kd_kurikulum']) : '';
		$kd_matakuliah = isset($_POST['kd_matakuliah']) ? strval($_POST['kd_matakuliah']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		$kd_kelas = isset($_POST['kd_kelas']) ? strval($_POST['kd_kelas']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.nim, b.nama, c.id_krs_detail, c.nilai_uas,c.nilai_uts,c.nilai_tugas,c.nilai_quis,c.presensi,c.nilai_final,c.nilai_huruf, 
			e.bobot_nilai_tugas,e.bobot_nilai_uts,e.bobot_nilai_uas,e.bobot_presensi,e.bobot_quis, f.TglNilai', false);
			$this->db->from('tbl_krs_detail c');
			$this->db->join('tbl_krs a', 'a.id_krs = c.id_krs');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('tbl_jadwal d', 'd.id_jadwal = c.id_jadwal');
			$this->db->join('tbl_pembobotan e', 'e.kd_matakuliah = c.kd_matakuliah');
			$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran and f.kd_prodi = a.kd_prodi');
			// if($kd_fakultas != '')
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('b.kd_prodi', $kd_prodi);
			//if($id_tahunajaran != '')
				$this->db->where('d.kd_matakuliah ', $kd_matakuliah);
				$this->db->where('d.kd_dosen ', $kd_dosen);
				$this->db->where('d.kd_kelas ', $kd_kelas);
				$this->db->where('c.nilai_huruf != "K"');
				
			$this->db->where('c.status', 'A');
			
			$this->db->order_by('a.nim', 'asc');
			//$this->db->order_by('a.JamMulai', 'asc');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	//Tambahan
	
	#buat input
	function getdata_input($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'c.id_krs_detail';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		//$kd_kurikulum = isset($_POST['kd_kurikulum']) ? strval($_POST['kd_kurikulum']) : '';
		$kd_matakuliah = isset($_POST['kd_matakuliah']) ? strval($_POST['kd_matakuliah']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		$kd_kelas = isset($_POST['kd_kelas']) ? strval($_POST['kd_kelas']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.nim, b.nama, c.id_krs_detail, c.nilai_uas,c.nilai_uts,c.nilai_tugas,c.nilai_quis,c.presensi,c.nilai_final,c.nilai_huruf, 
			e.bobot_nilai_tugas,e.bobot_nilai_uts,e.bobot_nilai_uas,e.bobot_presensi,e.bobot_quis, f.TglNilai', false);
			$this->db->from('tbl_krs_detail c');
			$this->db->join('tbl_krs a', 'a.id_krs = c.id_krs');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('tbl_jadwal d', 'd.id_jadwal = c.id_jadwal');
			$this->db->join('tbl_pembobotan e', 'e.kd_matakuliah = c.kd_matakuliah');
			$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran and f.kd_prodi = a.kd_prodi');
			// if($kd_fakultas != '')
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('b.kd_prodi', $kd_prodi);
			//if($id_tahunajaran != '')
				$this->db->where('d.kd_matakuliah ', $kd_matakuliah);
				$this->db->where('d.kd_dosen ', $kd_dosen);
				$this->db->where('d.kd_kelas ', $kd_kelas);
				$this->db->where('c.nilai_huruf != "K"');
				
			$this->db->where('c.status', 'A');
			
			$this->db->order_by('a.nim', 'asc');
			//$this->db->order_by('a.JamMulai', 'asc');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	
	function getdata2($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'c.id_krs_detail';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		//$kd_kurikulum = isset($_POST['kd_kurikulum']) ? strval($_POST['kd_kurikulum']) : '';
		$kd_matakuliah = isset($_POST['kd_matakuliah']) ? strval($_POST['kd_matakuliah']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		$kd_kelas = isset($_POST['kd_kelas']) ? strval($_POST['kd_kelas']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.nim, b.nama, c.id_krs_detail, c.nilai_uas,c.nilai_uts,c.nilai_tugas,c.nilai_quis,c.presensi,c.nilai_final,c.nilai_huruf, 
			e.bobot_nilai_tugas,e.bobot_nilai_uts,e.bobot_nilai_uas,e.bobot_presensi,e.bobot_quis, f.TglNilai', false);
			$this->db->from('tbl_krs_detail c');
			$this->db->join('tbl_krs a', 'a.id_krs = c.id_krs');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('tbl_jadwal d', 'd.id_jadwal = c.id_jadwal');
			$this->db->join('tbl_pembobotan e', 'e.kd_matakuliah = c.kd_matakuliah');
			$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran and f.kd_prodi = a.kd_prodi');
			// if($kd_fakultas != '')
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('b.kd_prodi', $kd_prodi);
			//if($id_tahunajaran != '')
				$this->db->where('d.kd_matakuliah ', $kd_matakuliah);
				$this->db->where('d.kd_dosen ', $kd_dosen);
				$this->db->where('d.kd_kelas ', $kd_kelas);
				$this->db->where('c.nilai_huruf != "K"');
				
			$this->db->where('c.status', 'A');
			
			$this->db->order_by('a.nim', 'asc');
			//$this->db->order_by('a.JamMulai', 'asc');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	function Update_Nilai($data){
		$this->db->trans_start();
		
		$result = true;
		
		# tambah sap ke tabel
		foreach($data['data_nilai']['rows'] as $row){
			
			$this->db->flush_cache();
			$this->db->set('nilai_tugas', $row['nilai_tugas']);
			$this->db->set('nilai_uts', $row['nilai_uts']);
			$this->db->set('nilai_uas', $row['nilai_uas']);
			$this->db->set('nilai_quis', $row['nilai_quis']);
			$this->db->set('presensi', $row['presensi']);
			$this->db->set('nilai_final', $row['nilai_final']);
            $this->db->set('nilai_huruf', $row['nilai_huruf']);
			/*
			//hitung nilai akhir dan grade
			$nilai_tugas = $row['nilai_tugas'];
			$nilai_uts = $row['nilai_uts'];
			$nilai_uas = $row['nilai_uas'];
			$nilai_quis = $row['nilai_quis'];
			$presensi = $row['presensi'];
			
			$bobot_tugas = $row['bobot_nilai_tugas'];
			$bobot_uts = $row['bobot_nilai_uts'];
			$bobot_uas = $row['bobot_nilai_uas'];
			$bobot_quis = $row['bobot_quis'];
			$bobot_presensi = $row['bobot_presensi'];
			
			$nilai_final = (($nilai_tugas*$bobot_tugas/100)+($nilai_uts*$bobot_uts/100)+($nilai_uas*$bobot_uas/100)+($presensi*$bobot_presensi/100)+($nilai_quis*$bobot_quis/100));
			//$this->db->set('nilai_final', $nilai_final);
			
			if(($nilai_final >= 85)&&($nilai_final <= 100)){
				$nilai_huruf = 'A';
				//$this->db->set('nilai_huruf', $nilai_huruf);
			}
			else if(($nilai_final >= 75)&&($nilai_final <= 80)){
				$nilai_huruf = 'B';
				//$this->db->set('nilai_huruf', $nilai_huruf);
			}
			else if(($nilai_final >= 60)&&($nilai_final <= 74)){
				$nilai_huruf = 'C';
				//$this->db->set('nilai_huruf', $nilai_huruf);
			}
			else if(($nilai_final >= 40)&&($nilai_final <= 59)){
				$nilai_huruf = 'D';
				//$this->db->set('nilai_huruf', $nilai_huruf);
			}
			else if(($nilai_final >= 1)&&($nilai_final <= 39)){
				$nilai_huruf = 'E';
				//$this->db->set('nilai_huruf', $nilai_huruf);
			}
			else if($nilai_final = 0){
				$nilai_huruf = 'E';
				//$this->db->set('nilai_huruf', $nilai_huruf);
			}
			
			$nilai_grade = $nilai_huruf;
			
			$this->db->set('nilai_final', $nilai_final);
			$this->db->set('nilai_huruf', $nilai_grade); */

			$this->db->where('id_krs_detail', $row['id_krs_detail']);

			$result = $this->db->update('tbl_krs_detail');
			
		}
		
		//return
		$this->db->trans_complete();
	    return $this->db->trans_status();
	}
	
	function getMatakuliah($kd_matakuliah){
		$this->db->flush_cache();
		$this->db->select('nama_matakuliah, sks');
		$this->db->from('ref_matakuliah');
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$res = $this->db->get();
		
		return $res;
	}
	
	function DeleteOnDb($id){
		//query delete data		
		$this->db->flush_cache();
		$this->db->set('status', 'D');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_jadwal', $id);
		
		$result = $this->db->update('tbl_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	//untuk PDF
	/* function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.nim, b.nama, c.id_krs_detail, c.nilai_uas,c.nilai_uts,c.nilai_tugas,c.presensi,c.nilai_final,c.nilai_huruf,d.kd_kelas,d.sks', false);
			$this->db->from('tbl_krs_detail c');
			$this->db->join('tbl_krs a', 'a.id_krs = c.id_krs');
			$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
			$this->db->join('tbl_jadwal d', 'd.id_jadwal = c.id_jadwal');
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('b.kd_prodi', $fil['kd_prodi']);
			//if($id_tahunajaran != '')
				$this->db->where('d.kd_matakuliah ', $fil['kd_matakuliah']);
				$this->db->where('d.kd_dosen ', $fil['kd_dosen']);
				$this->db->where('d.kd_kelas ', $fil['kd_kelas']);
				
				$this->db->where('c.status', 'A');

		// proses
		$result = $this->db->get();
		
		# init column
		$pdfdata['column'] = array();
		array_push($pdfdata['column'], array('caption'=>'No.', 'width'=>10, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nim', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nama', 'width'=>100, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'SKS', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		//array_push($pdfdata['column'], array('caption'=>'Semester', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		array_push($pdfdata['column'], array('caption'=>'Nilai', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		//array_push($pdfdata['column'], array('caption'=>'IPS', 'width'=>30, 'align'=>'L', 'val_align'=>'L'));
		
		# init record
		$i=1;
		$pdfdata['row'] = array();
		foreach($result->result() as $row){
			array_push($pdfdata['row'], array(
				$i,
				$row->nim,
				$row->nama,
				$row->sks,
				$row->nilai_huruf
			));
			$i++;
		}
		
		return $pdfdata;
		
	} */
	
	function getNameByKodeMK($fil){
		$this->db->flush_cache();
		$this->db->from('ref_matakuliah');
		$this->db->order_by('nama_matakuliah');
		$this->db->where('kd_matakuliah', $fil);
		
		$res = $this->db->get()->row();
		
		return $res->nama_matakuliah;
	}
	
	function getNameByKoDosen($fil){
		$this->db->flush_cache();
		$this->db->from('ref_dosen');
		$this->db->order_by('nama_dosen');
		$this->db->where('kd_dosen', $fil);
		
		$res = $this->db->get()->row();
		
		return $res->nama_dosen;
	}
	
	//PDF
	function get_pdf($fil){
		
		# get data
		$this->db->flush_cache();
		$this->db->start_cache();			
		$this->db->select('a.nim, b.nama, c.id_krs_detail, c.nilai_uas,c.nilai_uts,c.nilai_tugas,c.nilai_quis,c.presensi,c.nilai_final,c.nilai_huruf, 
		e.bobot_nilai_tugas,e.bobot_nilai_uts,e.bobot_nilai_uas,e.bobot_presensi,e.bobot_quis, f.TglNilai', false);
		$this->db->from('tbl_krs_detail c');
		$this->db->join('tbl_krs a', 'a.id_krs = c.id_krs');
		$this->db->join('tbl_mahasiswa b', 'b.nim = a.nim');
		$this->db->join('tbl_jadwal d', 'd.id_jadwal = c.id_jadwal');
		$this->db->join('tbl_pembobotan e', 'e.kd_matakuliah = c.kd_matakuliah');
		$this->db->join('tbl_tahunajaran f', 'f.id_tahunajaran = a.id_tahunajaran and f.kd_prodi = a.kd_prodi');
		// if($kd_fakultas != '')
		
		// if($kd_fakultas != '')
			// $this->db->where('a.kd_fakultas', $kd_fakultas);
		//if($kd_prodi != '')
		$this->db->where('b.kd_prodi', $fil['kd_prodi']);
		//if($id_tahunajaran != '')
		$this->db->where('d.kd_matakuliah ', $fil['kd_matakuliah']);
		$this->db->where('d.kd_dosen ', $fil['kd_dosen']);
		$this->db->where('d.kd_kelas ', $fil['kd_kelas']);
			
		$this->db->where('c.status', 'A');

		// proses
		$result = $this->db->get();
		
		if ($result->num_rows() > 0) {
            foreach ($result->result() as $data) {
                $data_pdf[] = $data;
            }
            return $data_pdf;           
        }
	}
	
}

?>